package com.sense.feel.login.model

import com.sense.feel.login.model.bean.SigInfo
import com.sense.feel.login.model.net.LoginApi
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.repo.bean.ConfigBean
import com.sense.kukoo.common.repo.http.KukooHttpManager
import com.sense.kukoo.common.repo.bean.ResultBean
import okhttp3.RequestBody
import com.sense.kukoo.common.repo.bean.UserInfo

/**
 * @Author Lionszhang
 * @Date 2021/4/3 14:38
 * @Name MainModel.java
 * @Instruction MainActivity层
 */
class LoginModel : BaseMvvmModel() {

    /**第三方登录*/
    suspend fun login(
        avatarUrl: String, loginChannelKind: String, nickName: String, userId: String
    ): ResultBean<UserInfo> {

        val hashMap = HashMap<String, Any>()
        hashMap.put("avatarUrl", avatarUrl)
        hashMap.put("loginChannelKind", loginChannelKind)
        hashMap.put("userId", userId)
        hashMap.put("nickname", nickName)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return KukooHttpManager.instance.createHttpClient(LoginApi::class.java)
            .reportUserInfo(mRequestBody)
    }

    /**手机号登录*/
    suspend fun login(
        hasInvitedFive: Boolean, verifyCode: String, phoneNumber: String, signUpUniqueCode: String,
        inviterUnique: String, keyUuid: String
    ): ResultBean<UserInfo> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("hasInvitedFive", hasInvitedFive)
        hashMap.put("inviterUnique", inviterUnique)
        hashMap.put("keyUuid", keyUuid)
        hashMap.put("signUpUniqueCode", signUpUniqueCode)
        hashMap.put("verifyCode", verifyCode)
        hashMap.put("phoneNumber", phoneNumber)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return KukooHttpManager.instance.createHttpClient(LoginApi::class.java)
            .login(mRequestBody)
    }

    suspend fun getConfig(
        env: String, region: String
    ): ResultBean<ConfigBean>? {
        val hashMap = HashMap<String, Any>()
        hashMap.put("evn", env)
        hashMap.put("region", region)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return KukooHttpManager.instance.createHttpClient(LoginApi::class.java)
            .getConfigInfo(mRequestBody)
    }

    /**获取腾讯云签名信息*/
    suspend fun getImSig(
        token: String?
    ): ResultBean<SigInfo>? {
        val hashMap = HashMap<String, Any>()
        hashMap.put("token", token ?: "")

        return KukooHttpManager.instance.createHttpClient(LoginApi::class.java)
            .getImSig(hashMap)
    }

    suspend fun getVerifyCode(
        phoneNumber: String,
        rand: String,
        ticket: String
    ): ResultBean<String>? {
        val hashMap = HashMap<String, Any>()
        hashMap.put("phoneNumber", phoneNumber)
        hashMap.put("rand", rand)
        hashMap.put("ticket", ticket)
        hashMap.put("source", "Login")
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return KukooHttpManager.instance.createHttpClient(LoginApi::class.java)
            .getVerifyCode(mRequestBody)
    }

    override fun onModelCleared() {}
}